var loading = false;
var maxItems = 100;

var itemsPerLoad = 20;

function addItems(number, lastIndex) {
    var html = '';
    for (var i = lastIndex + 1; i <= lastIndex + number; i++) {
        html += '<li class="item-content"><div class="item-inner"><div class="item-title">Item ' + i + '</div></div></li>';
    }
    $('.list-container').append(html);

}
addItems(itemsPerLoad, 0);


var lastIndex = 20;

$(document).on('infinite', '.infinite-scroll',function() {

    // 如果正在加载，则退出
    if (loading) return;

    // 设置flag
    loading = true;

    setTimeout(function() {
        loading = false;

        if (lastIndex >= maxItems) {
            $.detachInfiniteScroll($('.infinite-scroll'));
            $('.infinite-scroll-preloader').remove();
            return;
        }

        addItems(itemsPerLoad, lastIndex);
        lastIndex = $('.list-container li').length;
    }, 1000);
});